package engine.newUi
{
	import com.greensock.TweenLite;
	
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ObjectData;
	import engine.data.ResPath;
	import engine.data.RoleVo;
	import engine.data.View;
	import engine.data.ViewObject;
	import engine.event.SoundEvent;
	import engine.flag.GameDefine;
	import engine.flag.ResourcesPath;
	import engine.logic.DieHelpLogic;
	import engine.logic.DoubleClickManager;
	import engine.logic.LogSendLogic;
	import engine.logic.RequestForCost;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.tip.tipItem.KungfuItem;
	import engine.ui.ActorPartnerGroup;
	import engine.ui.AlertUI;
	import engine.ui.UIManage;
	import engine.util.FilterUtil;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import newx.BaseObject;
	import newx.StageInfo;
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.group.NxViewGroup;
	import newx.com.control.window.NxWindowEvent;
	import newx.com.loader.SwfLoader;
	import newx.utils.BitmapAPI;
	import newx.utils.MouseHandle;

	public class KungfuPackLogic
	{
		
		/**
		 *属性名 
		 */		
		private const DATAOBJ:String="dataobj";
		private const INDEX:String="index";
		private const ITEM:String="item",ADD_DATA:String="addData",BG:String="mc1",NEED:String="need",CONDITION:String="condition",TIPS:String="tips",LELVE:String="mc2",BB:String="mc";
		private const TXT:String="titleTxt2";
		private const LVL:String="infoTxt5";
		private const BIND:String="bind";
		private const NAME_TITLE:String="txt";
//		private const LEVEL_TITLE:String="infoTxt";
		private const LOCK_DATA:String="lockIndex",BTN:String="btn_1",BTN_LAB:String="txt1";
		private const TOOLSIZE_PRO:String="KungFuToolBoxSize";
		private const BIND_LEVEL:String="bind_level";
		
		private const CONFIGID:String="ConfigID";
		private const VIEW_ID:String="_viewid";
		private const PHOTO:String="EffectivePhoto"
		
		private const NORMAL:int	=	1;
		private const ROLL:int		=	2;
		private const DISABLE:int	=	3;
		private const DISABLE_ROLL:int	=	4;
		
		private var _bind:BindManage;
		private var _model:ModelLocator
		private var _role:RoleVo;
		private var _tip:TipsMainLogic;
		private var _mouse:MouseHandle;
		private var _doubleClickManager:DoubleClickManager;
		private var _actorGroup:ActorPartnerGroup;
		private var _kungfuitem:KungfuItem;
		private var _ui:KungfuPackUI;
		private var _rollItem:MovieClip;
		private var _levelObj:Object;
		private var _dragIcon:MovieClip;
		private var _exp:int;
		private var _kungfuEffect:MovieClip;
		private var _kungfuArr:Array;
		
		private var _bindKungfu:int=-1;
		private var _bindKungfuTool:int=-1;
		private var _bindKungfuToolCount:int=-1;
		private var _bindKungfuCount:int=-1;
		private var _bindReminCountFlag:int=-1;
		private var _bindExpFlag:int=-1;
		private var _bindFigthFlag:int=-1;
		private var _bindPowerFlag:int=-1
		private var _eqObjViewflag:int;
		/**
		 *装备长度 
		 */		
		private var _len:int;
		
		private var _unlock:int
		private var _lockIndex:int
		
		private var _initBeforeShow:Boolean=false;
		private var photoMc:MovieClip;
		private var _glow:GlowFilter = new GlowFilter(0x0a2529, 1, 2, 2, 3, BitmapFilterQuality.LOW);
		public function get ui():KungfuPackUI
		{
			if(_ui==null)
			{
				_ui=new KungfuPackUI
			}
			return _ui;
		}
		private static var _self:KungfuPackLogic;
		public static function getInstance():KungfuPackLogic
		{
			if(_self==null)
			{
				_self=new KungfuPackLogic();
			}
			return _self;
		}
		
		public function  Init():void
		{
			_bind=BindManage.getInstance();
			_model=ModelLocator.getInstance();
			_role=_model.scene.Role;
			_tip=TipsMainLogic.instance;
			_mouse=MouseHandle.getInstance();
			_doubleClickManager	= DoubleClickManager.getInstance();
			_kungfuitem=new KungfuItem;
			_actorGroup=new ActorPartnerGroup(ui.actorGroup,setCurrentMC);
			
//			_ui.composeBtnlab.buttonMode=true;
//			FilterUtil.mouseOverOutLab(_ui.composeBtnlab,"00ff00",_tip.getColorHtml(StaticIni.getSwfLangStr("Kungfu_UIString2"),"ffff00"));
			_ui.dragLab.mouseEnabled=false;
			_ui.dragArea.addFrameScript(_ui.dragArea.totalFrames-1,function():void{
				_ui.dragArea.gotoAndStop(1);
			})
//			_ui.bgpic.source=ResPath.PNG+"kungfuBG.png";
			_dragIcon=ResManager.newResClass(StaticIni.getIniVar("KungfuPackUI_packGroup","create").split(",")[3]) as MovieClip;
			_kungfuEffect=ResManager.newResClass("custSkin_227") as MovieClip;
			_kungfuEffect[BIND]=function(parent:DisplayObjectContainer,dp:DisplayObject):void{
				if(!_kungfuEffect.parent&&parent)
				{
					_kungfuEffect.visible=true;
					_kungfuEffect.mouseChildren=_kungfuEffect.mouseEnabled=false;
					_kungfuEffect.x=dp.width/2+4;
					_kungfuEffect.y=dp.height/2-5;
					parent.addChild(_kungfuEffect);
					_kungfuEffect.gotoAndPlay(1);
				}
			}
			_kungfuEffect.addFrameScript(_kungfuEffect.totalFrames-1,function():void{
				_kungfuEffect.visible=false;
				_kungfuEffect.stop();
				if(_kungfuEffect.parent)
				{
					_kungfuEffect.parent.removeChild(_kungfuEffect);
				}
			})
			_dragIcon.visible=false;
//			_ui.oneClickLab.htmlText=StaticIni.getSwfLangStr("Kungfu_UIString2");//"一键合成";
//			_ui.gotoLab.htmlText=StaticIni.getSwfLangStr("Kungfu_UIString3")//");去扔色子";
			_ui.exchangeBtn.mouseEnabled=_ui.exchangeBtn.buttonMode=true;
			FilterUtil.mouseOverOutLab(_ui.exchangeBtn,"00ff00",StaticIni.getSwfLangStr("Kungfu_UIString4"));
			
			var tempObj:Object=StaticIni.getIniObj("PlayerLevelBox");
			_levelObj=new Object;
			for  (var j:String in tempObj) 
			{
				_levelObj[tempObj[j]]=j
			}
			forEachKungfuOn(function(m:MovieClip,i:int):void{
				m[INDEX]=i+1;
				m[NAME_TITLE].htmlText="";
//				(m[NAME_TITLE] as TextField).autoSize=TextFieldAutoSize.CENTER;
				(m[BTN_LAB] as TextField).autoSize=TextFieldAutoSize.CENTER;
				m[BTN_LAB].wordWrap=false
//				m[LEVEL_TITLE].htmlText="";
				m[BTN_LAB].mouseEnabled=m[NAME_TITLE].mouseEnabled=/*m[LEVEL_TITLE].mouseEnabled=*/false;
				m[BTN_LAB].htmlText=StaticIni.getSwfLangStr("Common_UpGrade");
				m[ITEM]=new NxClip;
//				m[BG]	=	new NxClip;
//				m[ITEM].source="custSkin_0030";
				m[BG]["face"].addChild(m[ITEM]);
//				m["p_mc"]["img01"].addChild(m[ITEM]);
				
				//检查格子状态 是否变化和得到tips
				m[CONDITION]=function():Boolean
				{
					var	b:Boolean;
					if(!this[DATAOBJ])
						b= false;
					else
					{
						_kungfuitem.dataObj=this[DATAOBJ];
						this[NEED]=_kungfuitem.needExpToNext;
					 	b	=	_exp>=this[NEED];
					}
					this[BTN].enabled=b;
					this[TIPS]= b?StaticIni.getSwfLangStrVar("Kungfu_UIString17",[m[NEED]]):StaticIni.getSwfLangStrVar("Kungfu_UIString17",[_tip.getColorHtml(m[NEED],"ff0000")]);
					this[BTN].filters=b?null:[FilterUtil.GrayColorFilter];
					this[BTN_LAB].filters=b?[_glow]:[FilterUtil.GrayColorFilter,_glow];
					return b;
				}
				//向格子添加数据，并且根据数据  显示图片文字
				m[ADD_DATA]=function (data:ObjectData=null):void
				{
					this[DATAOBJ]=data;
					if(data)
					{
						this[ITEM].source=ResPath.PNG+"2"+getStaticProperty(data,"Photo")+".png";
						(this[BG] as MovieClip).gotoAndStop(getStaticProperty(data,"BookType"));
						this[BG].visible=true;
						_kungfuitem.dataObj=data;
						this[NAME_TITLE].htmlText=_kungfuitem.getColorName();
						this[BTN].visible=true;
						this[BTN_LAB].htmlText=StaticIni.getSwfLangStr("Common_UpGrade")
						this[LELVE].visible=this[BTN].visible=this["txt1"].visible=true;
						if(this[BIND_LEVEL]==null)
						{
							this[BIND_LEVEL]=function (lvl:int):void{
//								_kungfuitem.dataObj=this[DATAOBJ]
//								this[LEVEL_TITLE].htmlText=_kungfuitem.getLevel2();
								this[LELVE].gotoAndStop(lvl+1);
								if(lvl==9)
								{
									this[BTN].visible=false;
									this[BTN_LAB].htmlText=StaticIni.getSwfLangStr("UIString_1000270")
								}
								this[CONDITION]();
							}
						}
						this[BIND]	=_bind.BindPropInViewObj(data.parent as View,data.ObjKey,"KungFuLevel",BindFunc,null,[this,BIND_LEVEL]);
					}
					else
					{
						this[ITEM].source=null;
						this[BG].visible=false;
						this[NAME_TITLE].htmlText=""
						this[LELVE].visible=	this[BTN].visible=this["txt1"].visible=false;
//						this[LEVEL_TITLE].htmlText=""
					}
					this[CONDITION]();
				}
				m[ADD_DATA](null);
				m.addEventListener(MouseEvent.MOUSE_DOWN,beginDrag)
				m.addEventListener(MouseEvent.CLICK,click);
				m.addEventListener(MouseEvent.MOUSE_OVER,rollOver);
				m.addEventListener(MouseEvent.ROLL_OUT,rollout);
			});
			var length:int=_ui.packGroup.col*_ui.packGroup.row;
			for (var i:int = 0; i < length; i++) 
			{
				var mc:MovieClip=_ui.packGroup.getItemByIndex(i);
				initPackItem(mc);
				mc[INDEX]=i+1;
				mc[VIEW_ID]=View.VIEWPORT_KUNGFUTOOL;
				mc.addEventListener(MouseEvent.MOUSE_DOWN,beginDrag)
				mc.addEventListener(MouseEvent.CLICK,click)
				mc.addEventListener(MouseEvent.ROLL_OVER,rollOver);
				mc.addEventListener(MouseEvent.ROLL_OUT,rollout);
			}
			initPackItem(_dragIcon);
			_dragIcon[NORMAL]=function (dp:DisplayObject):void
			{
				_dragIcon.x=dp.x;
				_dragIcon.y=dp.y;
				dp.parent.addChild(this);
			}
//			initBG();
//			length=_ui.bgGroup.col*_ui.bgGroup.row;
//			for ( i = 0; i < length; i++) 
//			{
//				mc=_ui.bgGroup.getItemByIndex(i);
//				mc[INDEX]=i;
//			}
			function initPackItem(mc:MovieClip):void
			{
				mc[ITEM]= new NxClip;
				//				mc[BG]	=	new NxClip;
				mc[BG]["face"].addChild(mc[ITEM]);
				//				mc[ITEM].source="custSkin_0030";
				//				mc[ITEM].content["face"].addChild(mc[BG]);
				mc[ADD_DATA]=function (data:ObjectData=null):void
				{
					this[DATAOBJ]=data;
					if(data)
					{
						this[ITEM].source=ResPath.PNG+"2"+getStaticProperty(data,"Photo")+".png";
						(this[BG] as MovieClip).gotoAndStop(getStaticProperty(data,"BookType"));
						this[BG].visible=true;
						if(this[BIND_LEVEL]==null)
						{
							this[BIND_LEVEL]=function (lvl:int):void{
								//								this[LEVEL_TITLE].htmlText=_kungfuitem.getLevel2();
								this[LELVE].gotoAndStop(lvl+1);
							}
						}
						_kungfuitem.dataObj=this[DATAOBJ]
						if(this.hasOwnProperty(BIND)&&this[BIND]!=-1)
						{
							this[BIND]	=_bind.UnbindProp(data,"KungFuLevel",this[BIND]);
						}
						this[BIND]	=_bind.BindPropInViewObj(data.parent as View,data.ObjKey,"KungFuLevel",BindFunc,null,[this,BIND_LEVEL]);
						this[LELVE].visible=true;
						this[NAME_TITLE].htmlText=_kungfuitem.getColorName();
					}
					else
					{
						this[ITEM].source=null;
						this[BG].visible=false;
						this[LELVE].visible=false;
						this[NAME_TITLE].htmlText=""
					}
				}
				mc[ADD_DATA](null)
			}
			function getStaticProperty(data:ObjectData,proName:String):String
			{
				var configid:int=data.GetPropertiesEx("ConfigID");
				return StaticIni.getIniVar(configid,proName);
			}
			_doubleClickManager.addDoubleClick( KungfuPackLogic, singleClick, doubleClick );
		}
		private function initBG():void
		{
			const url:String=ResPath.SWF+"kungfuBG.swf";
			if(!_ui.bgpic.content)
			{
				SwfLoader.StartLoad(url, null, f);
			}
			function f(mc:MovieClip,...args):void
			{
				_ui.bgpic.source=mc;
			}
		}
		public function	BeforeShow():void
		{
//			_ui.bgpic.source=ResPath.PNG+"kungfuBG.png";
			initBG();
			_initBeforeShow=true;
			_actorGroup.BeforeShow()
			addAllEventListener()
			refreshUI();
			_initBeforeShow=false;
		}
		public function	AfterHide():void
		{
			removeAllEventListener();
			_actorGroup.AfterHide();
			forEachKungfuOn(function(mc:MovieClip,i:int):void{
				if(mc[ITEM]&&mc[ITEM].content)
					removeDataToMc(mc,mc[INDEX])
			})
			ui.packGroup.getItemForEach(function(mc:MovieClip):void{
				if(mc[ITEM]&&mc[ITEM].content)
				{
					removeDataToMc(mc,mc[INDEX])
				}
			})
		}
		private function tweenAll(group:NxViewGroup):void
		{
			var len:int=group.col*group.row;
			for (var i:int = 0; i < len; i++) 
			{
				var b:Boolean	=	tweenSingle(group.getItemByIndex(i));
//				if(!b)break;
			}
			
		}
		public function getBitMapInSamePoint(dp:DisplayObject):Bitmap
		{
			var bitmap:Bitmap=BitmapAPI.getBitmap(dp);
			StageInfo.stage.addChild(bitmap);
			var point:Point=dp.parent.localToGlobal(new Point(dp.x,dp.y));
			bitmap.x=point.x;
			bitmap.y=point.y;
			return bitmap;
		}
		private function tweenSingle(mc:MovieClip):Boolean
		{
//			var mc:MovieClip=_ui.packGroup.getItemByIndex(index);
//			mc.visible=false;
			if(mc[DATAOBJ])
			{
				var dp:DisplayObject=mc//McPool.borrowRes(mc[DATAOBJ].GetPropertiesEx(PHOTO))//mc[BG2].content;
			}
			if(dp==null)
			{
//				reset();
				return false;
			}
			var bitmap:Bitmap=getBitMapInSamePoint(dp);
			var target:DisplayObject=_ui.dragArea;
			var point:Point=target.parent.localToGlobal(new Point(target.x+target.width/2,target.y+target.height/2))
			//			TweenLite.to(bitmap,0.3,{blurFilter:{blurX:5,blurY:5},onComplete:move});
			move();
			return true;
			function move():void
			{
				TweenLite.to(bitmap,1,{x:point.x	,	y:point.y	,scaleX:0.1,scaleY:0.1,onComplete:disappear})
			}
			function disappear():void
			{
				bitmap.parent.removeChild(bitmap);
//				reset();
			}
		}
		/**
		 *背包内所有武学的经验 
		 * @return 
		 * 
		 */		
		private function get allexp():int
		{
			var view:View=_model.scene.GetView(View.VIEWPORT_KUNGFUTOOL);
			var obj:Object=view.Properties;
			var totalExp:int	=	0;
			for  (var i:String in obj) 
			{
				if(Number(i))
				{
					_kungfuitem.dataObj=obj[i];
					totalExp+=_kungfuitem.kungfuInitExp;
					totalExp+=_kungfuitem.kungfuCurrentExp;
				}
			}
			return totalExp;
		}
		/**
		 *按钮毁掉 
		 * @param e
		 * 
		 */		
		private function clickBtnHandle(e:Event):void
		{
			switch(e.currentTarget)
			{
				case _ui.composeBtn:
				{
					AlertUI.showAlert(" ",StaticIni.getSwfLangStrVar("Kungfu_UIString8",[allexp]),AlertUI.YES|AlertUI.NO,function createGuild( t:int, str:String, clickData:* ):void
					{
						if(AlertUI.YES == t )
						{
							SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ADDEXP_KUNGFU_AUTO);
							_model.dispatchEvent(new SoundEvent(SoundEvent.COMBINE_ITEMS));
							tweenAll(_ui.packGroup);
						}
					}
					)
					break;
				}
				case _ui.exchangeBtn:
				{
					ExChangePowerLogic.getInstance().ui.showOrHide();
					break;
				}
				case _ui.gotoBtn:
				{
					if(WipeOutLogic.getInstance().WipeState)
					{
						NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("97855"));//扫荡中不能打开
						return
					}
					else if(CampBattleLogic.getInstance().isJoinCamp)
					{
						NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("99198"));//阵营战开启中
						return;
					}
					
					if(!DiceLogic.getInstance().ui.created)
					{
						UIManage.getInstance().requestOpenUI(DiceLogic.getInstance().ui);
//						DiceLogic.getInstance().ui.showOrHide();
					}
					close(null);
					break;
				}
					
				default:
				{
					break;
				}
			}
		}
		private function close(e:Event):void
		{
			_ui.hide();
		}
		public function addAllEventListener():void
		{
			if( _bindKungfuTool == -1 )
			{
				_bindKungfuTool		=	_bind.BindView(_model.scene,View.VIEWPORT_KUNGFUTOOL,OnAddTool,OnRemoveTool);
			}
			if(_bindKungfuToolCount	==	-1)
			{
				_bindKungfuToolCount	 = _bind.BindProp(_model.scene.GetView(View.VIEWPORT_KUNGFUTOOL),TOOLSIZE_PRO,this,"toolunlock")
			}
			if(_bindReminCountFlag==-1)
			{
				_bindReminCountFlag= _bind.BindProp(_role,"DefaultCount",this,"remainCount")
			}
			if(_bindExpFlag==-1)
			{
				_bindExpFlag= _bind.BindProp(_role,"PlayerKungFuExp",this,"expCount")
			}
			_bindFigthFlag=_bindFigthFlag==-1?_bind.BindProp(_role, ActorPropertyUI.TOTAL_FIGHT_POWER,this,"fightPower"):_bindFigthFlag
//			_ui.bgGroup.addItemsEvent(MouseEvent.CLICK,lockHandle);
			_ui.composeBtn.addEventListener(MouseEvent.CLICK,clickBtnHandle);
			_ui.gotoBtn.addEventListener(MouseEvent.CLICK,clickBtnHandle);
			_ui.exchangeBtn.addEventListener(MouseEvent.CLICK,clickBtnHandle);
			_ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,close);
			_ui.kungfuMc.addEventListener(MouseEvent.ROLL_OVER,function (e:Event):void{
				var arr:Array=_kungfuArr;
				if(!(arr&&arr.length))return ;
				var 	str:String="";
				arr.sort(
					function compare(v1:BaseObject,v2:BaseObject):int
					{
						if(v1.GetProperties("KungFuQuality")==v2.GetProperties("KungFuQuality"))
						{
							return v2.GetProperties("KungFuLevel")-v1.GetProperties("KungFuLevel")
						}
						else
						{
							return v2.GetProperties("KungFuQuality")-v1.GetProperties("KungFuQuality")
						}
					}
				)
				for each (var val:ViewObject in arr) 
				{
					_kungfuitem.data=(val as ViewObject).Properties;
					str+=_kungfuitem.getLittleStr()+"\n";
				}
				ui.kungfuMc.gotoAndStop(2);
				if(str)
					_tip.showWideString(str);
			})
			_ui.kungfuMc.addEventListener(MouseEvent.ROLL_OUT,function(e:Event):void{
				ui.kungfuMc.gotoAndStop(1);
				_tip.hide();
			})
		}
		private function lockHandle(e:Event):void
		{
			var mc:MovieClip=e.currentTarget as MovieClip;
			if(mc.currentFrame==DISABLE)
			RequestForCost.getInstance().request(4,openPackageByindex,(mc[INDEX]+1)*5);
		}
		public function removeAllEventListener():void
		{
			if( _bindKungfuTool != -1 )
			{
				_bindKungfuTool		=	_bind.UnbindView(View.VIEWPORT_KUNGFUTOOL,_bindKungfuTool);
			}
			if(_bindKungfuToolCount != -1)
			{
				_bindKungfuToolCount	 = _bind.UnbindProp(_model.scene.GetView(View.VIEWPORT_KUNGFUTOOL),"KungFuToolBoxSize",_bindKungfuToolCount)
			}
			if(_bindReminCountFlag != -1)
			{
				_bindReminCountFlag	 = _bind.UnbindProp(_role,"DefaultCount",_bindReminCountFlag)
			}
			if(_bindExpFlag != -1)
			{
				_bindExpFlag	 = _bind.UnbindProp(_role,"PlayerKungFuExp",_bindExpFlag)
			}
			_bindFigthFlag=_bindFigthFlag!=-1?_bind.UnbindProp(_role, "PlayerKungFuExp",_bindFigthFlag):_bindFigthFlag
//			_ui.composeBtnlab.removeEventListener(MouseEvent.CLICK,clickBtnHandle);
			_ui.gotoBtn.removeEventListener(MouseEvent.CLICK,clickBtnHandle);
			_ui.exchangeBtn.removeEventListener(MouseEvent.CLICK,clickBtnHandle);
			_ui.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,close);
		}
		/**
		 *魂力？ 
		 * @param value
		 * 
		 */		
		public function set powerCount(value:int):void
		{
//			_ui.funcCountLab.htmlStr=StaticIni.getSwfLangStrVar("Kungfu_UIString6",[value])//"功力： "+value;
		}
		public function set expCount(value:int):void
		{
			if(!_initBeforeShow&&value>_exp)
			{
				_ui.dragArea.gotoAndPlay(3);
				FilterUtil.animationText(_ui.dragLab,{htmlStrByBorder:value});
			}
			else
			{
				_ui.dragLab.htmlStrByBorder=""+value;
			}
			_exp=value;
			forEachKungfuOn(function(mc:MovieClip,i:int):void
			{
				mc[CONDITION]();
			}
			)
			
		}
		public function set remainCount(value:int):void
		{
			_ui.leavCountLab.htmlStr=StaticIni.getSwfLangStrVar("Kungfu_UIString5",[value])//"残卷数量："+value;
		}
		public function set toolunlock(value:int):void
		{
//			trace("KungfuPackLogic.toolunlock(value)",value);
			var $this:Object=this;
			//			_ui.packGroup.getItemForEach(lock,[value])
			var length:int=_ui.packGroup.col*_ui.packGroup.row;
			lock(value,length,e);
//			lock(value/_ui.packGroup.col-1,_ui.bgGroup.row,ed)
			function e(i:int):MovieClip
			{
				return $this._ui.packGroup.getItemByIndex(i);
			}
			function ed(e:int):MovieClip
			{
				return $this._ui.bgGroup.getItemByIndex(e);
			}
		}
		private function lock(value:int,length:int,func:Function):void
		{
			for (var i:int = 0; i <length; i++) 
			{
				var mc:MovieClip=func(i)//_ui.packGroup.getItemByIndex(i);
				mc[BB].gotoAndStop(i>=value?DISABLE:NORMAL)
//				mc.visible=i<value;
				if(mc[BB].currentFrame==DISABLE)
				{
					if(_levelObj.hasOwnProperty(i+1))
					{
						mc[LOCK_DATA]=_levelObj[i+1];
					}
					else
					{
						mc[LOCK_DATA]="Kungfu_UIString20";
					}
				}
			}
		}
		public function set kungfuUnlock(value:int):void
		{
			lock(value,_len,getKungfubyIndex);
			
		}
		private function getKungfuViewId(partnerID:int):int
		{
			var arr:Array=MyPropertyLogic.getInstance().getPartnerViewsById(partnerID);
			if(arr&&arr.length)
				return arr[4]		
			return 0;
		}
		private function getKungfubyIndex(index:int):MovieClip
		{
//			trace("KungfuPackLogic.getKungfubyIndex(index)",index);
			return _ui.eqGroup.getItemByIndex(index)
//			if(_ui.hasOwnProperty("mc_"+index))
//				return _ui["mc_"+index];
//			return null;
		}
		private function setCurrentMC(value:MovieClip):void
		{
			currentSelectedM=value;
		}
		
		/**
		 * 设置是否需要指引打开 获得心法的UI
		 * 
		 */		
		private function setLeadOpenUI():void
		{
			var roleVo:RoleVo = _model.scene.Role;
			if(isEquipKungfu())
			{
				this._ui.tiShi.visible = false;
				this._ui.tiShi.gotoAndStop(1);
			}
			else
			{
				this._ui.tiShi.visible = true;
				this._ui.tiShi.gotoAndPlay(1);
			}	
		}
		
		public function get currentSelectedM():MovieClip
		{
			return _actorGroup.currentMC;
		}
		
		public function set currentSelectedM(value:MovieClip):void
		{
			if(_bindPowerFlag != -1)
			{
				_bindPowerFlag	 = _bind.UnbindProp(_actorGroup.currentData,"PlayerKungFuTotalExp",_bindPowerFlag)
			}
			refreshUI();
		}
		/**
		 *刷新界面 
		 * 
		 */		
		private function refreshUI():void
		{
			clearAllKungFu();
			var vo:BaseObject=		_actorGroup.currentData;
			var model:String=vo.GetProperties(ActorPropertyUI.MODEL);
			photo=model;
			typeStr=vo.GetProperties(ActorPropertyUI.GANG);
			nameStr=vo is RoleVo?vo.GetProperties(ActorPropertyUI.NAME):StaticIni.getSwfLangStr(vo.GetProperties("ConfigID"));
			lvlStr=StaticIni.getSwfLangStr(vo.GetProperties(ActorPropertyUI.LEVEL));
			refreshBind();
			refreshKungfu();
			setLeadOpenUI();
		}
		private function removeKungFu():void
		{
			if(_eqObjViewflag)
			{
				if( _bindKungfu != -1 )
				{
					_bindKungfu		=	_bind.UnbindView(_eqObjViewflag,_bindKungfu);
				}
				if(_bindKungfuCount != -1)
				{
					_bindKungfuCount	 = _bind.UnbindProp(_model.scene.GetView(_eqObjViewflag),"KungFuBoxSize",_bindKungfuCount)
				}
			}
			_kungfuArr=[];
		}
		public function get isHasKungfu():Boolean
		{
			var kungfuTool:Object=_model.scene.GetView(View.VIEWPORT_KUNGFUTOOL).Properties;
			for  (var j:String in kungfuTool) 
			{
				if(Number(j))
				{
					return true;
				}
			}
			return false;
		}
		public function isAllViewHasEmpty():Boolean
		{
			var roleKungfu:View=_model.scene.GetView(View.VIEWPORT_KUNGFU);
			if(!roleKungfu)
				return false;
			else
			{
				if(isViewIdHasEmpty(roleKungfu))
					return true;
			}
			var obj:Object=_model.scene.GetView(View.VIEWPORT_PARTNER).Properties;
			for (var i:String in obj) 
			{
				if(Number(i))
				{
					if(isViewIdHasEmpty(_model.scene.GetView(getKungfuViewId(int(obj[i].GetPropertiesEx("ConfigID"))))))
						return true
				}
			}
			return false
		}
		
		/**
		 *是否装备心法 
		 * @return 
		 * 
		 */
		public function isEquipKungfu():Boolean
		{
			var roleKungfu:View=_model.scene.GetView(View.VIEWPORT_KUNGFU);
			if(roleKungfu.Properties.hasOwnProperty(1))
			{
				return true;
			}
			var partnerKunfu:Object=_model.scene.GetView(View.VIEWPORT_PARTNER).Properties;
			for (var i:String in partnerKunfu) 
			{
				if(Number(i))
				{
					var Kunfu:View = _model.scene.GetView(getKungfuViewId(int(partnerKunfu[i].GetPropertiesEx("ConfigID"))));
					if(Kunfu)
					{
						if(Kunfu.Properties.hasOwnProperty(1))
						{
							return true;
						}
					}
				}
			}
			return false;
		}
		
		private function isViewIdHasEmpty(view:View):Boolean
		{
			//			var id:int//=getKungfuViewID();
			//			id=viewid==0?View.VIEWPORT_KUNGFU:getKungfuViewId(viewObj.GetPropertiesEx("ConfigID"));
			//			var view:View=_model.scene.GetView(id);
			var len:int;
			if(!view)return false;
			for (var i:String in view.Properties) 
			{
				if(Number(i))
					len++;
			}
			return len<view.GetPropertiesEx("KungFuBoxSize");
		}
		private function refreshBind():void
		{
			removeKungFu();	
			if(_bindKungfuCount	==	-1)
			{
				var id:int=getKungfuViewID();
				var view:View=_model.scene.GetView(id)
				if(!view)
				{
					kungfuUnlock=0;
				}
				else
				{
					_bindKungfuCount	 = _bind.BindProp(view,"KungFuBoxSize",this,"kungfuUnlock")
				}
			}
			
			if( _bindKungfu == -1 )
			{
				_bindKungfu		=	_bind.BindView(_model.scene,getKungfuViewID(),OnAddKungfu,OnRemoveKungfu);
			}
			if(_bindPowerFlag==-1)
			{
				_bindPowerFlag= _bind.BindProp(_actorGroup.currentData,"PlayerKungFuTotalExp",this,"powerCount")
			}
		}
		private function clearAllKungFu():void
		{
			forEachKungfuOn(removeDataToMc);
		}
		/**
		 *function（mc:movieClip,index:i）:void{} 
		 * @param f
		 * 
		 */		
		private function forEachKungfuOn(f:Function):Array
		{
			var i:int=0;
			var mc:MovieClip=getKungfubyIndex(i);
			var arr:Array=[]
			while(mc)
			{
				var o:Object=f(mc,i);
				if(o)
					arr.push(o);
				mc=getKungfubyIndex(++i)
			}
			_len=i;
			return arr
		}
		/**
		 *得到当前装备功法的viewid 
		 * @return 
		 * 
		 */		
		public function getKungfuViewID():int
		{
			//绑定装备
			if(_actorGroup.currentIndex	==	0)
			{
				_eqObjViewflag=	View.VIEWPORT_KUNGFU
			}
			else 
			{
				_eqObjViewflag=getKungfuViewId(_actorGroup.rollArr[_actorGroup.currentIndex].GetPropertiesEx(CONFIGID));
			}
			return _eqObjViewflag;
		}
		private function OnAddTool(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			PropertiesValue.HandlePropertiesEx(VIEW_ID,View.VIEWPORT_KUNGFUTOOL)
			addToolByIndex(PropertiesValue,int(PropertiesName)-1);
		}
		private function addToolByIndex(propertyValue:ViewObject,index:int):void
		{
			var mc:MovieClip=_ui.packGroup.getItemByIndex(index);
			if(mc)
				addDataToMc(propertyValue,mc);
			else
				trace("Error~FuncPowerLogic.addToolByIndex(propertyValue, index)",index,"");
			
		}
		private function addDataToMc(value:ViewObject,mc:MovieClip):void
		{
			mc[ADD_DATA](value);
		}
		private function BindFunc(PropertiesName:String,PropertiesValue:*,pList:Array):void
		{
			var		Obj:Object	=	pList[0];
			var		value:*		=	pList[1];
			Obj[value](PropertiesValue);	
		}
		public function get rollItem():MovieClip
		{
			return _rollItem;
		}
		
		public function set rollItem(value:MovieClip):void
		{
			if(_rollItem)
			{
				if(_rollItem[BB].currentFrame==ROLL)
					_rollItem[BB].gotoAndStop(NORMAL);
				else if(_rollItem[BB].currentFrame	==	DISABLE_ROLL)
					_rollItem[BB].gotoAndStop(DISABLE)
			}
			_rollItem = value;
			if(_rollItem)
			{
				if(_rollItem[BB].currentFrame==NORMAL)
					_rollItem[BB].gotoAndStop(ROLL);
				else if(_rollItem[BB].currentFrame	==	DISABLE)
					_rollItem[BB].gotoAndStop(DISABLE_ROLL);
			}
		}
		private function click(e:MouseEvent):void
		{
			var mc:MovieClip=(e.currentTarget as MovieClip) as MovieClip;
			if(mc[DATAOBJ]	&&	e.target==mc[BTN]	&&	mc[BTN].enabled)
			{
				sendADDex(mc[DATAOBJ]);
				//动画特效
				_kungfuEffect[BIND](mc,mc[BG]);
			}
			else
			{
				_doubleClickManager.onClick(KungfuPackLogic,e);
			}
			
			
		}
		private function singleClick(e:Event):void
		{
			var mc:MovieClip=(e.currentTarget as MovieClip) as MovieClip;
			if((mc[BB].currentFrame==DISABLE	||	mc[BB].currentFrame==DISABLE_ROLL)	&&	mc[VIEW_ID])
			{
				RequestForCost.getInstance().request(4,openPackageByindex,mc[INDEX]);
			}
			
		}
		private function openPackageByindex(index:int):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_AFFIRM_CHARGE,4,index)
			LogSendLogic.getInstance().btnClickNumSend(22807)
		}
		private function doubleClick(e:Event):void
		{
			var mc:MovieClip=e.currentTarget as MovieClip;
			var m2:MovieClip;
			if(!mc[DATAOBJ])return;
			if(mc[VIEW_ID])
			{
				var arr:Array=forEachKungfuOn(getKungfu);
				if(!arr||!arr.length)return 
					m2	=	arr[0];
			}
			else
			{
				m2	=	getFirstEmptyTool();
			}
			if(m2)
			{
				swap(mc[DATAOBJ],m2);
			}
			
		}
		private function getKungfu(mc:MovieClip,i:int):MovieClip
		{
			if(!mc[DATAOBJ]&&mc.currentFrame!=DISABLE)
			{
				return mc;
			}
			return null
		}
		private function getFirstEmptyTool():MovieClip
		{
			var len:int=_ui.packGroup.col*_ui.packGroup.row;
			for (var i:int = 0; i < len; i++) 
			{
				var m:MovieClip=getKungfu(_ui.packGroup.getItemByIndex(i),i);
				if(m)
					return m
			}
			return null
			
		}
		private function beginDrag(e:Event):void
		{
			var mc:MovieClip=e.currentTarget as MovieClip;
//			var bp:Bitmap=BitmapAPI.getBitmap(mc);
			if(mc&&mc[DATAOBJ]&&mc[ITEM].content)
			{
				if(mc.hasOwnProperty(BTN)&&e.target==mc[BTN])
				{
					return ;
				}
//				var p:Point=mc.parent.localToGlobal(new Point(mc.x,mc.y));
//				StageInfo.stage.addChild(bp)
//				mc.addChild(bp);
//				bp.x=p.x;
//				bp.y=p.y;
				_dragIcon[ADD_DATA](mc[DATAOBJ]);
				_dragIcon[NORMAL](mc);
				var bit:Bitmap=getBitMapInSamePoint(_dragIcon);
				_mouse.setMouseDrag(bit,true,mc)
				bit.parent.removeChild(bit);
//				trace("KungfuPackLogic.beginDrag(e)",);
//				mc.removeChild(bp);
				//				mc.addEventListener(MouseEvent.MOUSE_UP,endDrag)
				_ui.packGroup.addItemsEvent(MouseEvent.MOUSE_UP,endDrag);
				forEachKungfuOn(addE);
				_ui.dragArea.addEventListener(MouseEvent.MOUSE_OVER,dragOver);
				_ui.dragArea.addEventListener(MouseEvent.MOUSE_UP,endDrag);
			}
			function addE(mc:MovieClip,i:int):void
			{
				mc.addEventListener(MouseEvent.MOUSE_UP,endDrag);
			}
		}
		private function dragOver(e:Event):void
		{
			_ui.dragArea.gotoAndStop(2);
			_ui.dragArea.addEventListener(MouseEvent.MOUSE_OUT,dragOut);
		}
		private function dragOut(e:Event):void
		{
			_ui.dragArea.gotoAndStop(1);
		}
		private function endDrag(e:Event):void
		{
			_ui.dragArea.removeEventListener(MouseEvent.MOUSE_OVER,dragOver);
			_ui.dragArea.removeEventListener(MouseEvent.MOUSE_OUT,dragOut);
			_ui.dragArea.removeEventListener(MouseEvent.MOUSE_UP,endDrag);
			_ui.packGroup.removeItemsEvent(MouseEvent.MOUSE_UP,endDrag);
			forEachKungfuOn(removeE)
			var mc:MovieClip=e.currentTarget as MovieClip;
			if(_ui.dragArea.getBounds(_ui.stage).contains(StageInfo.stage.mouseX,StageInfo.stage.mouseY))
			{
				if(_mouse.targetData[VIEW_ID])
					sendDispose(_mouse.targetData[DATAOBJ]);
			}
			else	/*if(mc&&mc[DATAOBJ])
			{
				//融合
				swallow(_mouse.targetData as ViewObject,mc[DATAOBJ] as ViewObject)
			}
			else*/
			if(_mouse.targetData!=mc)
			{
				//移动
				swap(_mouse.targetData[DATAOBJ] as ViewObject,mc)
			}
			_mouse.removeDrag();
			function removeE(mc:MovieClip,i:int):void
			{
				mc.removeEventListener(MouseEvent.MOUSE_UP,endDrag);
			}
			
		}
		/**
		 *分解 
		 * @param s1
		 * 
		 */		
		private function sendDispose(s1:ObjectData):void
		{
//			trace("KungfuPackLogic.sendADDex(s1)","分解");
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_DECOMPOSE_KUNGFU,int(s1.ObjKey));
		}
		/**
		 *增加经验 
		 * @param s1
		 * 
		 */		
		private function sendADDex(s1:ObjectData):void
		{
//			trace("KungfuPackLogic.sendADDex(s1)","增加经验");
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ADDEXP_KUNGFU,s1.GetPropertiesEx(VIEW_ID),int(s1.ObjKey));
		}
		private function swap(v1:ViewObject,mc:MovieClip):void
		{
//			trace("KungfuPackLogic.swap(v1, mc)","移动");
			if(!v1)return;
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_MOVE_KUNGFU ,v1.GetPropertiesEx(VIEW_ID),int(v1.ObjKey),getTargetViewIdByMc(mc),int(mc[INDEX]));
		}
		private function getTargetViewIdByMc(mc:MovieClip):int
		{
			if(mc[VIEW_ID])
				return mc[VIEW_ID]
			return this.getKungfuViewID();
		}
		
		private function rollOver(e:Event):void
		{
			var mc:MovieClip=e.currentTarget as MovieClip;
			rollItem=mc;
			if(mc&&mc[DATAOBJ])
			{
					if(mc.hasOwnProperty(BTN)&& e.target==mc[BTN])
						_tip.showMain(mc[TIPS]);
					else 
						_tip.showMain(mc[DATAOBJ])
			}
			else if((mc[BB].currentFrame==DISABLE	||	mc[BB].currentFrame==DISABLE_ROLL)&&!_ui.packGroup.contains(mc))
			{
				if(isNaN(Number(mc[LOCK_DATA])))
					_tip.showMain(StaticIni.getSwfLangStr(mc[LOCK_DATA]))
				else
					_tip.showMain(StaticIni.getSwfLangStrVar("Kungfu_UIString10",[mc[LOCK_DATA]]))
			}
		}
		private function rollout(e:Event):void
		{
			rollItem=null;
			_tip.hide();
		}
		private function removeToolByIndex(index:int):void
		{
			var mc:MovieClip=_ui.packGroup.getItemByIndex(index);
			removeDataToMc(mc);
		}
		private function removeDataToMc(mc:MovieClip,i:int=0):void
		{
			mc[ADD_DATA](null);
		}
		private function OnRemoveTool(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			removeToolByIndex(int(PropertiesName)-1);
		}
		private function OnAddKungfu(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			PropertiesValue.HandlePropertiesEx(VIEW_ID,getKungfuViewID());
			addKungfuByIndex(PropertiesValue,int(PropertiesName)-1);
			if(_kungfuArr.indexOf(PropertiesValue)==-1)
				_kungfuArr.push(PropertiesValue);
			refreshKungfu();
		}
		/**
		 *刷新心法图标显示 
		 * 
		 */		
		private function refreshKungfu():void
		{
			_ui.kungfuMc.visible=_kungfuArr&&_kungfuArr.length;
		}
		private function addKungfuByIndex(propertyValue:ViewObject,index:int):void
		{
			var mc:MovieClip=getKungfubyIndex(index);
			addDataToMc(propertyValue,mc);
		}
		private function removeKungfuByIndex(index:int):void
		{
			var mc:MovieClip=getKungfubyIndex(index);
			removeDataToMc(mc);
		}
		private function OnRemoveKungfu(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			removeKungfuByIndex(int(PropertiesName)-1);
			var index:int=_kungfuArr.indexOf(PropertiesValue)
			if(index!=-1)
				_kungfuArr.splice(index,1);
			refreshKungfu();
		}
		/**
		 *战斗力 
		 * @param value
		 * 
		 */		
		public function set fightPower(value:int):void
		{
			_ui.commendLab.htmlStrByBorder=StaticIni.getSwfLangStrVar("Kungfu_UIString24",[value]);
		}
		public function set nameStr(value:String):void 
		{
			_ui.nameLab.htmlText="<b>"+_tip.getColorHtml(value,"ffdf8a")+"</b>";
		}
		public function set lvlStr(value:String):void 
		{
			_ui.lvlLab.htmlText=StaticIni.getSwfLangStrVar("Kungfu_UIString23",[value]);
		}
		public function set typeStr(value:int):void
		{
			ui.typeLab.htmlStrByBorder=GameDefine.PART_PROFTYPE(value);
//				StaticIni.getSwfLangStrVar("UIString_1000011",[GameDefine.PART_PROFTYPE(value)]);
		}
		public function set photo(value:String):void 
		{
			var roleSkinIndex:int	=	0;
//			var url:String	=	getUrl()
//			url	=	ResPath.ACTORS + url;
			var clothmodel:int;
			if((_actorGroup.currentData).GetPropertiesEx("ConfigID")){
				clothmodel = (_actorGroup.currentData).GetPropertiesEx("ConfigID") == 0 ? (_actorGroup.currentData).GetPropertiesEx(ActorPropertyUI.CLOTHE_MODEL) : 0;
			} else {
				clothmodel = (_actorGroup.currentData).GetPropertiesEx("ClotheModel") == 0 ? 1 : (_actorGroup.currentData).GetPropertiesEx("ClotheModel");
			}
			_ui.faceclip.source=ResourcesPath.getHeadPic(int(value), clothmodel);
//			ui.actorView.source=url;
			function getUrl():String
			{
				var head:String;
				var tails:String;
				if(_actorGroup.currentIndex!=0)
				{
					head	=	"npc_";
					tails	=	".swf";
				}
				else
				{
					roleSkinIndex	=	ModelLocator.getInstance().scene.Role.GetPropertiesEx("ClotheModel");
					roleSkinIndex	=	roleSkinIndex  == 0?1:roleSkinIndex;
					head	=	"role_";
					tails	=	"_"+roleSkinIndex+".swf";					
				}
				return head+value+tails;
			}
			
		}
		public function get isToolHasEmpty():Boolean
		{
			var obj:Object=_model.scene.GetView(View.VIEWPORT_KUNGFUTOOL).Properties;
			var _len:int;
			for  (var i:String in obj) 
			{
				if(Number(i))
				{
					_len++;
				}
			}
			if(_len==obj[TOOLSIZE_PRO])
				return false
			return true;
		}
		public function get isMoneyEnough():Boolean
		{
			return _role.getGoldCoin()>=7000
		}
		public function get checkGuide():int
		{
			if(isHasKungfu&&isAllViewHasEmpty())//如果武学背包内有武学且有空位，则提示装备武学；
			{
				//				trace("提示装备武学")
				return DieHelpLogic.KUNGFU_EQUIP;
			}
			if(isToolHasEmpty	&&	isMoneyEnough)//如果武学背包内没有武学且空位，则提示获取武学；
			{
				//				trace("提示获取武学；");
				return DieHelpLogic.KUNGFU_GET;
			}
			//如果没有武学空位，不再提示武学；
			//如果不提示武学，进入阵法引导；
			return -1;
		}
	}
}